folders = ["maarten"]

import os, sys

# add the folders with our awesome modules
for folder in folders:
    #sys.path.append("%s%s..%s%s" % (os.getcwd(), os.sep, os.sep, folder))
    sys.path.append("%s%s%s" % (os.getcwd(), os.sep, folder))

# import domination
from controller import Controller


class Agent(object):

    debug_color_solid = (255, 00, 229)
    debug_color = (255, 00, 229, 128)

    NAME = "AWESOME"

    def __init__(self, id, team, settings=None, field_rects=None,
                    field_grid=None, nav_mesh=None, blob=None, **kwargs):
        """ Each agent is initialized at the beginning of each game.
            The first agent (id==0) can use this to set up global variables.
            Note that the properties pertaining to the game field might not be
            given for each game.
        """
        self.id = id
        self.team = team
        self.mesh = nav_mesh
        self.grid = field_grid
        self.settings = settings
        self.goal = None

        self.callsign = '%s-%d'% (('BLU' if team == TEAM_BLUE else 'RED'), id)

        # initiate the shared object for the communication between the agents
        if id == 0:
            self.controller = self.__class__.controller = Controller(settings)
        self.controller.add_agent(self)

    def observe(self, observation):
        """ Each agent is passed an observation using this function,
            before being asked for an action. You can store either
            the observation object or its properties to use them
            to determine your action. Note that the observation object
            is modified in place.
        """
        self.observation = observation
        self.controller.process_observation(observation)

    def action(self):
        """ This function is called every step and should
            return a tuple in the form: (turn, speed, shoot)
        """

        # have the controller set the agents goal
        self.controller.set_agent_goal(self)

        # calculate the path to the goal
        (turn, speed) = self.find_path_to_goal() 

        if turn == None: 
            print "ERROR: could not find path"
            return (0, 0, False)



        # if there is an enemy in range we might want to act on that
        if self.enemy_in_range():
            (turn, speed, shoot) = self.determine_shoot()
        # if we do not shoot we want to override the behavior???
        if not(shoot):
            path = self.find_path_to_goal()

        # if a path has been found we want to go to there
        if path:
            (turn, speed) = path
        return (turn, speed, shoot)

    def find_path_to_goal(self):
        """ calculates the path to a goal
            goal is a (x, y) tuple
            returns (turn, speed) tuple when a path has been found
            returns false when no path has been found
        """
        path = find_path(self.observation.loc, self.goal,
            self.mesh, self.grid) 

        if path:
            dx = path[0][0] - self.observation.loc[0]
            dy = path[0][1] - self.observation.loc[1]
            turn = angle_fix(math.atan2(dy, dx) - self.observation.angle)
            if turn > self.settings.max_turn or turn < -self.settings.max_turn:
                shoot = False
                speed = speed *0.5
            speed = (dx**2 + dy**2)**0.5
            return (turn, speed)
        return (None, None)


